using System.Security;
using System.Security.Permissions;
namespace Net35.Xrm.Sdk.Client
{
	[SecuritySafeCritical]
	[SecurityPermission(SecurityAction.Demand, Unrestricted = true)]
	internal sealed class LiveIdUserNameSecurityTokenHandler : Microsoft.IdentityModel.Tokens.UserNameSecurityTokenHandler
	{
		public SecurityTokenResponse DeviceTokenResponse
		{
			get;
			set;
		}
		public override bool CanValidateToken
		{
			get
			{
				return true;
			}
		}
		public override bool CanWriteToken
		{
			get
			{
				return true;
			}
		}
		public LiveIdUserNameSecurityTokenHandler(SecurityTokenResponse token)
		{
			if (token != null)
			{
				this.DeviceTokenResponse = token;
			}
		}
		public override void WriteToken(System.Xml.XmlWriter writer, System.IdentityModel.Tokens.SecurityToken token)
		{
			System.IdentityModel.Tokens.UserNameSecurityToken userNameSecurityToken = token as System.IdentityModel.Tokens.UserNameSecurityToken;
			if (userNameSecurityToken != null)
			{
				token = new System.IdentityModel.Tokens.UserNameSecurityToken(userNameSecurityToken.UserName, userNameSecurityToken.Password, LiveIdAuthenticationConfiguration.UserNameTokenId);
			}
			base.WriteToken(writer, token);
			if (this.DeviceTokenResponse != null && this.DeviceTokenResponse.Response != null && this.DeviceTokenResponse.Response.RequestedSecurityToken != null)
			{
				writer.WriteStartElement("wsse", "BinarySecurityToken", LiveIdAuthenticationConfiguration.SecurityTokenSchema);
				writer.WriteAttributeString("ValueType", LiveIdAuthenticationConfiguration.DeviceTokenId);
				this.DeviceTokenResponse.Response.RequestedSecurityToken.SecurityTokenXml.WriteTo(writer);
				writer.WriteEndElement();
			}
		}
	}
}
